/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.core.modal;

import com.google.common.collect.Lists;
import com.intelligent.ispc.common.utils.StringUtils;

import javax.swing.table.AbstractTableModel;
import java.util.List;

/**
 * Created by tony.yu on 2016/8/26.
 * Update by cherry on 2016/09/26
 */
public class SearchItemTableModel extends AbstractTableModel {
    private String[] columns;
    private List<String> data;
    private String selectedName;
    private String cacheName;

    public SearchItemTableModel() {
    }

    public SearchItemTableModel(String[] colums) {
        this.columns = colums;
        data = Lists.newArrayList();
        cacheName = "";
        selectedName = "";
    }

    @Override
    public int getRowCount() {
        if (data == null) {
            return 0;
        } else {
            return data.size();
        }
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (data.isEmpty()) {
            return new Object();
        }

        return data.get(rowIndex);
    }

    @Override
    public void setValueAt(Object obj, int rowIndex, int columnIndex) {
        data.set(rowIndex, (String) obj);
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    @Override
    public Class<?> getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public void setData(List<String> names) {
        if (names == null) {
            this.data = Lists.newArrayList();
        } else {
            this.data = names;
        }

        fireTableDataChanged();
    }

    public List<String> getData() {
        return data;
    }

    public String getCacheName() {
        return cacheName;
    }

    public void setCacheName(String cacheName) {
        this.cacheName = cacheName;
    }

    public String getSelectedName() {
        return selectedName;
    }

    public void setSelectedName(String selectedName) {
        this.selectedName = selectedName;
    }

    public boolean isValidCatchData() {
        boolean result = true;

        if (StringUtils.isBlank(selectedName)) {
            return true;
        }
        if (selectedName.equals(cacheName)) {
            return true;
        } else {
            result = false;
        }

        return result;
    }
}
